<template>
  <div>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <!-- 姓名 -->
      <el-form-item label="姓名" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>

      <!-- 性别 -->
      <el-form-item label="性别" prop="gender">
        <el-radio-group v-model="ruleForm.gender">
          <el-radio label="男"></el-radio>
          <el-radio label="女"></el-radio>
        </el-radio-group>
      </el-form-item>

      <!-- 年龄 -->
      <el-form-item label="年龄" prop="age">
        <el-input-number v-model="ruleForm.age" :min="0" :max="150"></el-input-number>
      </el-form-item>

      <!-- 出生日期 -->
      <el-form-item label="出生日期" prop="birthDate">
        <el-date-picker type="date" placeholder="选择日期" v-model="ruleForm.birthDate"
          style="width: 100%;"></el-date-picker>
      </el-form-item>

      <!-- 手机号 -->
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="ruleForm.phone"></el-input>
      </el-form-item>

      <!-- 邮箱 -->
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="ruleForm.email"></el-input>
      </el-form-item>

      <!-- 地址 -->
      <el-form-item label="地址" prop="address">
        <el-input v-model="ruleForm.address"></el-input>
      </el-form-item>

      <!-- 爱好 -->
      <el-form-item label="爱好类型" prop="hobby">
        <el-checkbox-group v-model="ruleForm.hobby">
          <el-checkbox label="科幻片"></el-checkbox>
          <el-checkbox label="古风片"></el-checkbox>
          <el-checkbox label="动漫类"></el-checkbox>
          <el-checkbox label="动作类"></el-checkbox>
        </el-checkbox-group>
      </el-form-item>

      <!-- 是否在校学生 -->
      <el-form-item label="是否在校学生" prop="isStudent">
        <el-switch v-model="ruleForm.isStudent"></el-switch>
      </el-form-item>

      <!-- 备注信息 -->
      <el-form-item label="备注信息" prop="desc">
        <el-input type="textarea" v-model="ruleForm.desc"></el-input>
      </el-form-item>

      <!-- 操作按钮 -->
      <el-form-item>
        <el-button type="primary" @click="saveForm('ruleForm')">保 存</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        name: '',
        gender: '',
        age: '',
        birthDate: '',
        phone: '',
        email: '',
        address: '',
        hobby: [],
        isStudent: false,
        desc: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        gender: [
          { required: true, message: '请选择性别', trigger: 'change' }
        ],
        age: [
          { required: true, message: '请输入年龄', trigger: 'blur' },
          { type: 'number', min: 0, max: 150, message: '年龄范围应在0-150之间', trigger: 'blur' }
        ],
        birthDate: [
          { type: 'date', required: true, message: '请选择出生日期', trigger: 'change' }
        ],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          { type: 'email', message: '邮箱格式不正确', trigger: 'blur' }
        ],
        hobby: [
          { type: 'array', required: true, message: '请至少选择一个爱好', trigger: 'change' }
        ],
        isStudent: [],
        desc: [
          { required: true, message: '请填写备注信息', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    saveForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 此处可以添加保存逻辑，如调用API接口
          alert('保存成功！');
          console.log('当前表单数据:', this.ruleForm);
        } else {
          console.log('表单验证失败');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>